#include "cw_dataprocess.h"

short shortabs(short Num)
{
	if(Num<0)
		return -Num;
	return Num;
}

int intabs(int Num)
{
	if(Num<0)
		return -Num;
	return Num;
}

float floatabs(float Num)
{
	if(Num<0)
		return -Num;
	return Num;
}

long longabs(long Num)
{
	if(Num<0)
		return -Num;
	return Num;
}

double doubleabs(double Num)
{
	if(Num<0)
		return -Num;
	return Num;
}

int sgn(int Num)
{
	if(Num<0)
		return -1;
	else if(Num==0)
		return 0;
	else
		return 1;
}

int longsgn(long num)
{
	if(num>0)
		return 1;
	else if(num<0)
		return -1;
	else
		return 0;
}

int doublesgn(double num)
{
	if(num>0)
		return 1;
	else if(num<0)
		return -1;
	else
		return 0;
}

double doublemin(double x,double y)
{
	if(x<y)return x;
	return y;
}
double doublemax(double x,double y)
{
	if(x<y)return y;
	return x;
}

int intmin(int x,int y)
{
	if(x<y)return x;
	return y;
}
int intmax(int x,int y)
{
	if(x<y)return y;
	return x;
}
//数值解析函数，传入初始位置和结束符，获取数值,并且获得分号的位置
int Numerical_analysis(uint8_t *NumStartIndex,char endchar,uint8_t **header)
{
	int value = 0;
	BOOLDef NegFlag=false;
	while(1)
	{
		if (*NumStartIndex==endchar)break;
		if(*NumStartIndex=='-')
		{
			NegFlag	= true;
			NumStartIndex++;
			continue;
		}
		value *=10;
		value += (*NumStartIndex-'0');
		NumStartIndex++;
	}
	*header = ++NumStartIndex;
	if(NegFlag==true) value=-value;
	return value;
}


//浮点数值解析函数，传入初始位置和结束符，获取数值,并且获得分号的位置
float FloatNum_analysis(uint8_t *NumStartIndex,char endchar,uint8_t **header)
{
	float value = 0;
	float value1=0;
	int FraFlag = 0;//小数部分标志
	BOOLDef NegFlag = false;//负数标志
	while(1)
	{
		if (*NumStartIndex==endchar)break;
		if(*NumStartIndex=='.')
		{
			FraFlag=1;
			NumStartIndex++;
		}
		if(*NumStartIndex=='-')
		{
			NegFlag	= true;
			NumStartIndex++;
			continue;
		}
		if(FraFlag==0)
		{
			value *=10;
			value += (*NumStartIndex-'0');
		}
		else
		{
			value1 *=10;
			value1 += (*NumStartIndex-'0');
			FraFlag++;
		}
		NumStartIndex++;
	}
	*header = ++NumStartIndex;
	//是否有小数部分，若有更新
	if(FraFlag!=0) value=(value+value1/pow(10,FraFlag-1));
	//是否为负数，若是则更新
	if(NegFlag==true) value=-value;
	return value;
}
